<?php

class vH_Upload {

    public function init() {
        
    }

    function uploadFile2($model, $root_file = '', $to_dir = '', $new_name = NULL) {

        $filename = $_FILES[$model]['name'][$root_file];
        $file_tmp_name = $_FILES[$model]['tmp_name'][$root_file];
        $i = 0;
        $extension = '.' . end(explode('.', $filename));
        $file_name = substr($filename, 0, -strlen($extension));

        if ($file_name == '') {
            return '';
        }
        if (!$new_name)
            $file_tmp = $this->getFileName($file_name . $extension, $to_dir);
        else {
            $file_tmp = $new_name;
            $file_ex = end(explode('.', $new_name));
            $file_tmp1 = substr($new_name, 0, -(strlen($file_ex) + 1));
            $i = 1;
            while (file_exists($to_dir . $file_tmp)) {
                $file_tmp = $file_tmp1 . '-' . $i . '.' . $file_ex;
                $i++;
            }
        }
        //after uploading, file is stored in tmp folder. We copy it to destination $dest
        if (is_uploaded_file($file_tmp_name)) {
            if (!file_exists($to_dir)) {

//                echo $to_dir;
//                die;

                mkdir($to_dir);
                exec('chmod 777 ' . $to_dir);
            }
            if (copy($file_tmp_name, $to_dir . $file_tmp)) {
                return $file_tmp;
            }
        }
        return '';
    }

    function uploadFile($model, $root_file = '', $to_dir = '', $new_name = NULL) {

        $filename = $_FILES[$model]['name'][$root_file];
        $file_tmp_name = $_FILES[$model]['tmp_name'][$root_file];
        $i = 0;
        $extension = '.' . end(explode('.', $filename));
        $file_name = substr($filename, 0, -strlen($extension));

        if ($file_name == '') {
            return '';
        }
        if (!$new_name)
            $file_tmp = $this->getFileName($file_name . $extension, $to_dir);
        else {
            $file_tmp = $new_name;
            $file_ex = end(explode('.', $new_name));
            $file_tmp1 = substr($new_name, 0, -(strlen($file_ex) + 1));
            $i = 1;
            while (file_exists($to_dir . $file_tmp)) {
                $file_tmp = $file_tmp1 . '-' . $i . '.' . $file_ex;
                $i++;
            }
        }
        //after uploading, file is stored in tmp folder. We copy it to destination $dest
        if (is_uploaded_file($file_tmp_name)) {
            if (!file_exists($to_dir)) {
                mkdir($to_dir);
                exec('chmod 777 ' . $to_dir);
            }
            if (copy($file_tmp_name, $to_dir . $file_tmp)) {
                return $file_tmp;
            }
        }
        return '';
    }

    function uploadFile1($root_file = '', $tmp_file = '', $to_dir = '', $new_name = NULL) {
        $i = 0;
        $extension = '.' . end(explode('.', $root_file));
        $file_name = substr($root_file, 0, -strlen($extension));

        if ($file_name == '') {
            return '';
        }
        if (!$new_name)
            $file_tmp = $this->getFileName($file_name . $extension, $to_dir);
        else {
            $file_tmp = $new_name;
            $file_ex = end(explode('.', $new_name));
            $file_tmp1 = substr($new_name, 0, -(strlen($file_ex) + 1));
            $i = 1;
            while (file_exists($to_dir . $file_tmp)) {
                $file_tmp = $file_tmp1 . '-' . $i . '.' . $file_ex;
                $i++;
            }
        }
        //after uploading, file is stored in tmp folder. We copy it to destination $dest
        if (is_uploaded_file($tmp_file)) {
            if (!file_exists($to_dir)) {
                mkdir($to_dir);
                exec('chmod 777 ' . $to_dir);
            }
            if (copy($tmp_file, $to_dir . $file_tmp)) {
                return $file_tmp;
            }
        }
        return '';
    }

    function moveFile($root_file = '', $dest_from = NULL, $dest_to = NULL) {
        if ($root_file == '') {
            return '';
        }
        //if the file is exists, change the name of this file
        $i = 0;
        $file_name = substr($root_file, 0, -4);
        $extension = substr($root_file, -4);
        $file = $file_name;
        if (file_exists($dest_from . $root_file)) {
            if (!file_exists($dest_to)) {
                mkdir($dest_to);
                exec('chmod 777 ' . $dest_to);
            }

            $file_tmp = $this->getFileName($root_file, $dest_to);

            //after uploading, file is stored in tmp folder. We copy it to destination $dest
            copy($dest_from . $root_file, $dest_to . $file_tmp);
            $this->deleteFile($root_file, $dest_from);
            return $file_tmp;
        }
        return '';
    }

    function deleteFile($root_file = '', $dest_from = NULL) {
        if ($root_file == '') {
            return;
        }

        if (file_exists($dest_from . $root_file)) {
            unlink($dest_from . $root_file);
            // Delete thumb			
            $thumb = str_replace('thumb_', '', $root_file);
            if (file_exists($thumb))
                unlink($thumb);
        }
        return;
    }

    function getFileName($root_file = '', $dest = NULL, $thumb_ = '') {
        if ($thumb_)
            return $thumb_ . $root_file;
        $root_file = strtolower($root_file);
        $extension = substr($root_file, -4);
        //$name = date('Ymd');
        $name = time();
        $i = 0;
        $name_tmp = $name . '_' . $i;
        while (file_exists($dest . $name_tmp . $extension)) {
            $name_tmp = $name . '_' . $i;
            $i++;
        }
        if (!strstr($extension, '.'))
            $extension = '.' . $extension;
        return $name_tmp . $extension;
    }

}

?>